package com.ray.business.strategy.order.delete;

import cn.hutool.core.util.StrUtil;
import com.ray.business.builder.BusinessBuilder;
import com.ray.business.service.ProdBusinessInService;
import com.ray.business.service.ProdBusinessOutService;
import com.ray.business.service.ProdBusinessService;
import com.ray.magicBlock.Strategy;
import com.ray.magicBlock.anno.Block;
import com.ray.woodencreate.beans.LoginUser;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;

/**
 * @author bo shen
 * @Description: 订单商品数据
 * @Class: PurchaseInService
 * @Package com.ray.wms.strategy.in
 * @date 2020/6/8 15:05
 * @company <p>Ray快速开发平台</p>
 * @updateRecord time(修改时间)  author(修改人)   desc(修改内容)
 */
@Slf4j
@Block(group = "deleteBusiness", strategy = "in", desc = "删除业务")
public class InOrderDeleteService implements Strategy<String, LoginUser, String> {

    @Autowired
    private ProdBusinessService prodBusinessService;
    @Autowired
    private ProdBusinessInService prodBusinessInService;
    @Autowired
    private ProdBusinessOutService prodBusinessOutService;

    @Override
    public String execute(String businessCode, LoginUser loginUser) {
        BusinessBuilder businessBuilder = new BusinessBuilder();
        businessBuilder.appendCode(businessCode).appendEdit(loginUser).delete();
        //更新
        prodBusinessService.edit(businessBuilder.bulid(), loginUser);
        //还原对应出库的状态
        prodBusinessInService.listAll(businessCode, loginUser).forEach(prodBusinessIn -> {
            //还原数据
            if (StrUtil.isNotBlank(prodBusinessIn.getOutCode())) {
                prodBusinessOutService.cancelBusinessOut(prodBusinessIn.getOutCode(), loginUser);
            }
        });
        //删除原有的记录
        prodBusinessInService.deleteByBusinessCode(businessCode, loginUser);
        return businessBuilder.getCode();
    }
}
